﻿/*
 * Created by SharpDevelop.
 * User: ley
 * Date: 5/17/2010
 * Time: 11:20 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

namespace Ley.Common.Validation
{

	public static class ValidationExtensions
	{
		#region IsRequired
		
		public static ValidationRuleSet<TSubject, string> IsRequired<TSubject>(this ValidationRuleSet<TSubject, string> ruleset, string message)
		{
			ruleset.Satisfies(value => !string.IsNullOrEmpty(value), message);
			return ruleset;
		}
		
		public static ValidationRuleSet<TSubject, IList<TProperty>> IsRequired<TSubject, TProperty>(this ValidationRuleSet<TSubject, IList<TProperty>> ruleset, string message)
		{
			ruleset.Satisfies(value => value != null && value.Any(), message);
			return ruleset;
		}
		
		public static ValidationRuleSet<TSubject, List<TProperty>> IsRequired<TSubject, TProperty>(this ValidationRuleSet<TSubject, List<TProperty>> ruleset, string message)
		{
			ruleset.Satisfies(value => value != null && value.Any(), message);
			return ruleset;
		}
		
		public static ValidationRuleSet<TSubject, TProperty> IsRequired<TSubject, TProperty>(this ValidationRuleSet<TSubject, TProperty> ruleset, string message)
			where TProperty : class
		{
			return ruleset.Satisfies(value => value != default(TProperty), message);
		}
		
		#endregion
		
		#region MatchesExpression
		
		public static ValidationRuleSet<TSubject, string> MatchesExpression<TSubject, TProperty>(this ValidationRuleSet<TSubject, string> ruleset, string regEx, string message)
		{
			return ruleset.Satisfies(value => new Regex(regEx).IsMatch(value), message);
		}
		
		#endregion
		
		#region MaxLength
		
		public static ValidationRuleSet<TSubject, string> MaxLength<TSubject>(this ValidationRuleSet<TSubject, string> ruleset, int max, string message)
		{
			ruleset.Satisfies(value => value.Length <= max, message);
			return ruleset;
		}
		
		#endregion
		
		#region MinLength
		
		public static ValidationRuleSet<TSubject, string> MinLength<TSubject>(this ValidationRuleSet<TSubject, string> ruleset, int min, string message)
		{
			ruleset.Satisfies(value => !string.IsNullOrEmpty(value) && value.Length >= min, message);
			return ruleset;
		}
		
		#endregion
	}
}
